package cn.edu.ncut.cs.springboot.springbootmybatisplus.controller;

import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Course;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Score;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Student;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.service.ICourseService;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.service.IScoreService;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.service.IStudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Controller
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private IScoreService scoreService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private ICourseService courseService;
    @RequestMapping("listScore")
    public String listScore(Model model, @RequestParam(required = false) String sno) {
        QueryWrapper<Score> qw=new QueryWrapper<>();
        if (sno != null && !sno.isEmpty()) {
            qw.like("sno", sno);
        }
        List<Course> courseList=courseService.list();
        model.addAttribute("courseList",courseList);
        List<Student> studentList=studentService.list();
        model.addAttribute("studentList",studentList);
        List<Score> list=scoreService.list(qw);
        model.addAttribute("scores",list);
        return "admin-score-list";
    }
    @RequestMapping("saveScore")
    private String saveScore(String sno, String cno, Integer grade,Model model, HttpSession session) {
        QueryWrapper<Score> qw=new QueryWrapper<>();
        qw.eq("sno",sno);
        qw.eq("cno",cno);
        Score one=scoreService.getOne(qw);
        if(one!=null){
            model.addAttribute("msg","该学生已选择该课程");
            List<Student> studentList=studentService.list();
            List<Course> courseList=courseService.list();
            model.addAttribute("studentList",studentList);
            model.addAttribute("courseList",courseList);
            return "admin-score-save";
        }
        Score score=new Score();
        score.setSno(sno);
        score.setScore(grade);
        score.setCno(cno);
        scoreService.save(score);
        return "redirect:/score/listScore";
    }
    @RequestMapping("preUpdateScore/{sno}/{cno}")
    public String preUpdateScore(@PathVariable String sno,@PathVariable String cno, Model model){
        QueryWrapper<Score> qw=new QueryWrapper<>();
        qw.eq("sno",sno);
        qw.eq("cno",cno);
        Score score=scoreService.getOne(qw);
        model.addAttribute("score",score);
        return "admin-score-update";
    }
    @RequestMapping("updateScore")
    public String updateScore(Score score){
        boolean b=scoreService.update(score);
        return "redirect:/score/listScore";
    }
    @RequestMapping("delScore/{sno}")
    public String delScore(@PathVariable Integer sno){
        boolean b=scoreService.removeById(sno);
        return "redirect:/score/listScore";
    }
}
